
var previous_load_key;

//显示建议框
function suggest(evt)
{
	if (!evt) evt = window.event;
	
	var layer = $("suggests");
	if (evt.keyCode == 13)
	{
		if (!identify.value) return;
		if (identify.value == previous_load_key) return;
		
		previous_load_key = identify.value;
		
		if (layer.style.display == "none")
		{
			layer.style.display = "block";
			var p = position(identify);
			layer.style.top = p.top + p.height + "px";
			layer.style.left = p.left + "px";
		}
		load();
	}
	else if (evt.keyCode == 40 && layer.style.display == "block")	//上下键按动的时候
	{
		layer.focus();
		layer.selectedIndex = 0;
		select();
	}
}

//隐藏建议框
function hide(type)
{
	var evt = window.event || arguments.callee.caller.arguments[0];
	if (type == 1)
	{
		var src = evt.target || evt.srcElement;		
		if (src == identify) return;
	}
	else if (type == 2)
	{
		if (evt.keyCode != 13) return;
	}
	else
	{
		return;
	}
	
	var layer = $("suggests");
	layer.style.display = "none";	
}

//加载建议数据
function load()
{
	var sgt = $("suggests");
	
	var action = new Action("ListManagingVatUserInfo");
	action.identify = previous_load_key;
	var callback = function(result)
	{
		sgt.options.length = 0;
		if (result.list == null || result.list.length == 0)
		{
			var option = new Option("<没有找到可选纳税人>", -1);
			option.data = null;
			sgt.options.add(option);
		}
		else
		{
			for (var n in result.list)
			{
				var u = result.list[n];
				var item = u.identify + "(" + u.name + ")";
				var value = u.id;
				var option = new Option(item, value);
				
				option.data = u;
				sgt.options.add(option);
			}
		}
	}
	
	sgt.options.length = 0;
	var option = new Option("<正在查找，请稍候...>", -1);
	option.data = null;
	sgt.options.add(option);
	
	Rpc.call(action, callback);
}

//选择建议框值
function select()
{
	var sgt = $("suggests");
	var data = sgt.options[sgt.selectedIndex].data;
	
	if (data)
	{
		identify.value = data.identify;
		vatName.value = data.name;
		vatAddress.value = data.address;
		vatOrg.value = data.org.name;
	}
	else
	{
		vatName.value = "";
		vatAddress.value = "";
		vatOrg.value = "";
	}
	
	left();
}